<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item><router-link to="/basic/contract">合同管理</router-link></el-breadcrumb-item>
        <el-breadcrumb-item>编辑合同</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <basic-container>
      <el-form :model="categoryForm" label-width="120px" label-position="right">
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户">
              <select-customer-form 
                  :customerObj="{customerName: categoryForm.customerName, customerSn: categoryForm.customerSn}"
                  @changeCustomer="handleSelectCustomer"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户联系人" required>
              <el-select v-model="categoryForm.linkmanId" class="fill-width"  placeholder="请选择">
                <el-option
                  v-for="item in manList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="市场负责人">
              <el-select filterable v-model="categoryForm.directorId" class="fill-width"  placeholder="请选择">
                <el-option
                  v-for="item in list"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="8">
            <el-form-item label="合同编号">
              <el-input v-model="categoryForm.salesContractSn" class="form-input" placeholder="请输入">
              </el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="合同状态" required >
              <el-select v-model="categoryForm.contractStatus" class="fill-width" placeholder="请选择">
                <el-option
                  v-for="item in contract_status"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同金额" required>
              <el-input-number v-model="categoryForm.contractAmount" :precision="2" class="fill-width" placeholder="请输入"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="销售分润比例">
              <el-input-number v-model="categoryForm.salesRate" :precision="2" class="fill-width"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          
          <el-col :span="8">
            <el-form-item label="签约时间" required>
              <el-date-picker
                class="fill-width" 
                v-model="categoryForm.contractTime"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结束时间" required>
              <el-date-picker
                class="fill-width" 
                v-model="categoryForm.closedTime"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同描述">
              <el-input v-model="categoryForm.contractDesc"  type="textarea" class="form-input"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="条件条款">
              <el-input v-model="categoryForm.conditions" type="textarea" class="form-input"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="合同图片列表" required>
              <div v-if="categoryForm.photoList" class="thumbUrl" v-for="(item,index) in categoryForm.photoList">
                <img :src="item.picUrl" />
                <div class="hover_show">
                  <i class="el-icon-delete" @click="handlePictureRemove(index)"></i>
                </div>
              </div>
              <div class="select_img" @click="sel_img()" style="float: left;"><i class="el-icon-plus"></i></div>
              <div v-if="sleImg">
                <el-dialog title="选择图片" :visible.sync="sleImg" style="text-align: left;">
                  <sel-img-photogallery ref="selImg" type="02" @select="onSelectImgs"></sel-img-photogallery>
                </el-dialog>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <div>
            <el-button type="primary" size="medium" @click="save">保存</el-button>
            <el-button type="default" size="medium" @click="close">取消</el-button>
          </div>
        </el-form-item>
      </el-form>
    </basic-container>
   
  </div>
</template>

<script>
  import {getStore, setStore} from "@/util/store";
  import selImgPhotogallery from '@/components/pds/common/sel_img_photogallery'
  import {
    fetchList,
  } from '@/api/crm/customer'
  import {
    fetchList as linkman,
  } from '@/api/crm/customerlinkman' //联系人
  import { contractEdit, getDetail } from '@/api/crm/salescontract';
  import {
    mapGetters
  } from 'vuex'
  import {
    fetchList as teamList,
  } from '@/api/crm/marketemployee'
  import {fetchDict} from '@/util/util';
  import selectCustomerForm from '@/components/crm/select_customer_form';
  export default {
    data() {
      return {
        categoryForm:{
          photoList:[],
          position:'',
        },
        sleImg:false,
        //初始化坐标
        contract_status:[],
        custmoerList:[],
        manList:[],
        list:[]
      }
    },
    components: {  
      selImgPhotogallery, 
      selectCustomerForm,
    },
    computed: {
      ...mapGetters(['tagWel', 'tagList', 'tag', 'website']),
      tagLen() {
        return this.tagList.length || 0
      }
    },
    mounted() {

    },
    created(){
      fetchDict('scm_contract_status', this.contract_status);
      this.getList();
      this.init();
    },
    methods: {
      init(){
        let params ={};
        params.contractSn = JSON.parse(this.$route.query.res).salesContractSn;
        getDetail(params).then(response => {
          this.categoryForm = response.data.data;
          this.getManList(this.categoryForm.customerSn);
        })
      },
      //获取销售区域
      getList(page, params) {
        this.tableLoading = true
        fetchList({current: 1, size: 1000}).then(response => {
          this.custmoerList = response.data.data.records
        })
        teamList({current: 1, size: 1000}).then(response => {
          this.list = response.data.data.records
        })
      },
      async handleSelectCustomer(customerObj) {
        if(this.categoryForm.customerSn === customerObj.customerSn) return;
        this.$set(this.categoryForm, 'customerSn', customerObj.customerSn);
        this.$set(this.categoryForm, 'customerName', customerObj.customerName);
        this.categoryForm.linkmanId = '';
        this.getManList(customerObj.customerSn);
      },
      async getManList(customerSn) {
        let res = await linkman({current: 1, size: 100, customerSn});
        this.manList = res.data.data.records;
      },
      sel_img(){
        this.sleImg = true;
      },
      onSelectImgs(imgs){
        imgs.forEach((item)=>{
          this.categoryForm.photoList.push({id:item.id,picUrl:item.picUrl})
        })
        this.sleImg = false;
      },
      // 删除el-upload的图片
      handlePictureRemove(index) {
        this.categoryForm.photoList.splice(index,1)
      },
      //确定
      save(){
        let tmp = []
        this.categoryForm.photoList.forEach((item)=>{
          tmp.push(item.id);
        })
        this.categoryForm.photoList = tmp;
        let params = this.categoryForm;
        this.manList.forEach((item)=>{
          if(item.id == params.linkmanId){
            params.linkmanName = item.name;
          }
        })
        this.custmoerList.forEach((item)=>{
          if(item.customerSn == params.customerSn){
            params.customerName = item.customerName;
          }
        })
        contractEdit(params).then(response => {
          if(response.data.code == 0){
            this.$message.success('创建成功')
            let _this = this;
            setTimeout(function () {
              _this.close();
            },2000)
          }else{
            this.$message.success(response.data.msg)
          }
        })
      },
      close(){
        var str_before = location.href.split('#')[1];
        let { tag, key } = this.findTag(str_before);
        this.$store.commit('DEL_TAG', tag);
        this.$router.go(-1);
      },
      findTag(value) {
        let tag, key
        this.tagList.map((item, index) => {
          if (item.value === value) {
            tag = item
            key = index
          }
        })
        return { tag: tag, key: key }
      }
    }
  }
</script>

<style scoped lang="scss">
  .crumbs{
    margin: 20px 10px;
  }
  .thumbUrl{
    width: 150px;
    height: 150px;
    position: relative;
    line-height: 146px;
    float: left;
    margin-right: 15px;
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid #c0ccda;
    text-align: center;
  }
  .thumbUrl img{
    width: 100%;
    height: 100%;
  }
  .thumbUrl .hover_show{
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(0,0,0,0.5);
    width: 100%;
    height: 100%;
    display: none;
  }
  .thumbUrl:hover .hover_show{
    display: block;
  }
  .thumbUrl .hover_show i{
    color: #fff;
    margin: 0 10px;
    cursor: pointer;
  }
  .select_img{
    width: 148px;
    height: 148px;
    line-height: 146px;
    border: 1px dashed #c0ccda;
    background-color: #fbfdff;
    font-size: 28px;
    color: #8c939d;
    cursor: pointer;
    border-radius: 6px;
    text-align: center;
  }
</style>


